package com.neusoft.chess;

import java.awt.Graphics;
import java.awt.Image;
import java.awt.Point;
import java.awt.Toolkit;
import java.io.File;
import java.util.Map;

import javax.swing.JPanel;

public abstract class Chess {
    private String name;//名称
    private int x;//x的坐标位置，指定图片绘制位置
    private int y;//y的坐标位置，指定图片绘制位置
    private Point p;//坐标值，用于计算x和y
    private Point initP;//初始坐标位置，不可更改
    private static final int size = 30;//大小
    private static final int space = 40;//间隔
    private static final int margin = 20;//边距
    public static final int player_red = 0;//红方
    public static final int player_black = 1;//黑方
    private int player;//阵营
    //File.separator：生成路径分隔符，在win为\，在linux为/
    private String folder = "pic" + File.separator;
    private String suffix = ".png";

    public void draw(Graphics g, JPanel panel) {
        Image img = Toolkit.getDefaultToolkit().getImage(folder + name + player + suffix);
        g.drawImage(img, x, y, size, size, panel);
    }

    /**
     * 棋子选中效果
     *
     * @param g
     */
    public void drawSelectedRect(Graphics g) {
        Image img = Toolkit.getDefaultToolkit().getImage(folder + name + player + suffix);
        g.drawRect(x, y, size, size);
    }

    public Chess(String name, Point p, int player) {
        this.name = name;
        setP(p);
        this.player = player;
    }

    public void calXY() {
        x = margin - size / 2 + space * (p.x - 1);
        y = margin - size / 2 + space * (p.y - 1);
    }

    /**
     * 判断棋子能否从原坐标p移动目标点t
     *
     * @param t
     * @return
     */
    public abstract boolean isAbleMove(Point t, Map<Point, Chess> map);

    /**
     * 根据点击的坐标xy生成Point坐标
     *
     * @param x
     * @param y
     * @return
     */
    public static Point getPointFromXY(int x, int y) {
        Point p = new Point();
        p.x = (x - margin + size / 2) / space + 1;
        p.y = (y - margin + size / 2) / space + 1;

        return p;
    }

    /**
     * 反转坐标
     */
    public void reserve() {
        p.x = 10 - p.x;
        p.y = 11 - p.y;
        initP = new Point(p);
        setP(p);
    }

    //在类中属性一般是不允许直接在其它类中直接使用的，建议使用get和set方法来设置获取属性值
    public void setName(String name) {
        this.name = name;
    }

    public String getName() {
        return this.name;
    }

    public void setP(Point p) {
        this.p = p;
        if (initP == null) {
            initP = p;
        }
        calXY();
    }

    public Point getP() {
        return this.p;
    }

    public int getPlayer() {
        return player;
    }

    public void setPlayer(int player) {
        this.player = player;
    }

    /*
     * toString()方法是属于Object类的
     * 	由子类重写
     */
    @Override
    public String toString() {
        //super.toString()：调用父类中toString()方法
//		return super.toString();
        return "Chess [name=" + name + ", x=" + x + ", y=" + y + ", p=" + p + ", size=" + size + ", space=" + space
                + ", margin=" + margin + ", player=" + player + ", folder=" + folder + ", suffix=" + suffix + "]";
    }

    /**
     * 是否走直线：0：非法直线，3：x轴直线，4：y轴直线，1：正斜线, 2：走日字y，-1：走日字x
     *
     * @param t
     * @return
     */
    public int isLine(Point t) {
		System.out.println("isLine");
        if (p.y == t.y && p.x != t.x) {
            return 3;
        } else if (p.x == t.x && p.y != t.y) {
            return 4;
        } else if (Math.abs(p.x - t.x) == Math.abs(p.y - t.y)) {
            return 1;
        } else if (Math.abs(p.x - t.x) == 1 && Math.abs(p.y - t.y) == 2) {
            //y
            return 2;
        } else if (Math.abs(p.x - t.x) == 2 && Math.abs(p.y - t.y) == 1) {
            //x
            return -1;
        } else {
            return 0;
        }
    }

    /**
     * 只能前进
     *
     * @param t
     * @return
     */
    public boolean isForward(Point t) {
        if (initP.y < 6) {
            //上边
            if (t.y > p.y) {
                return true;
            }
        } else if (initP.y > 5) {
            if (t.y < p.y) {
                return true;
            }
        }

        return false;
    }

    /**
     * 是否后退
     *
     * @param t
     * @return
     */
    public boolean isBack(Point t) {
        if (initP.y < 6) {
            //上边
            if (t.y < p.y) {
                return true;
            }
        } else if (initP.y > 5) {
            if (t.y > p.y) {
                return true;
            }
        }

        return false;
    }

    /**
     * 判断自身是否过河
     *
     * @return
     */
    public boolean isOverRiver(Point t) {
		System.out.println("isOverRiver");
        if (initP.y < 6) {
            //上边
            if (t.y > 5) {
                return true;
            }
        } else if (initP.y > 5) {
            if (t.y < 6) {
                return true;
            }
        }

        return false;
    }

    /*
        获取起点p到目标点t的步数
     */
    public int getStepCount(Point t) {
		System.out.println("getStepCount");
        int line = isLine(t);
        if (line == 3) {
            //x轴
            return Math.abs(p.x - t.x);
        } else if (line == 4) {
            //y轴
            return Math.abs(p.y - t.y);
        } else if (line == 1) {
            return Math.abs(p.y - t.y);
        }

        return 0;
    }

    /**
     * 计算起点p到终点t之间有多少个棋子
     *
     * @param t
     * @return
     */
    public int getChessCount(Point t, Map<Point, Chess> map) {
        int line = isLine(t);
        int start = 0;//起点位置
        int end = 0;//终点位置
        int count = 0;//统计
        Point pt = new Point();
        if (line == 4) {
            //y
            if (t.y > p.y) {
                //从上往下
                start = p.y;
                end = t.y;
            } else if (t.y < p.y) {
                //从下往上
                start = t.y;
                end = p.y;
            }
            pt.x = p.x;
            for (int i = start + 1; i < end; i++) {
                pt.y = i;
                if (map.get(pt) != null) {
                    count++;
                }
            }
        } else if (line == 3) {
            //x
            if (t.x > p.x) {
                //从左到右
                start = p.x;
                end = t.x;
            } else if (t.x < p.x) {
                //从右到左
                start = t.x;
                end = p.x;
            }
            pt.y = p.y;
            for (int i = start + 1; i < end; i++) {
                pt.x = i;
                if (map.get(pt) != null) {
                    count++;
                }
            }
        }

        return count;
    }

    /**
     * 是否在王宫范围内
     *
     * @param t
     * @return
     */
    public boolean isInHome(Point t) {
        if (t.x < 4 || t.x > 6) {
            return false;
        }
        if (initP.y < 6) {
            //上边
            if (t.y < 1 || t.y > 3) {
                return false;
            }
        } else if (initP.y > 5) {
            if (t.y < 8 || t.y > 10) {
                return false;
            }
        }

        return true;
    }

    /**
     * 是否蹩脚
     *
     * @param t
     * @return
     */
    public boolean isBieJiao(Point t, Map<Point, Chess> map) {
        Point center = new Point();
        if ("xiang".equals(name)) {
            center.x = (p.x + t.x) / 2;
            center.y = (p.y + t.y) / 2;
        } else if ("ma".equals(name)) {
            int line = isLine(t);
            if (line == 2) {
                //y
                center.x = p.x;
                center.y = (p.y + t.y) / 2;
            } else if (line == -1) {
                //x
                center.y = p.y;
                center.x = (p.x + t.x) / 2;
            }
            System.out.println(center);
        }
        Chess c = map.get(center);
        System.out.println(c);

        return c != null;
    }
}
